/*
 *
 * Author: Bob Wei
 * Date:   2009/06/19
 * License:free
 * email:  bob61016@gmail.com
 *
 */
var STORE_NAME = "helloStore";
var filesToCapture = [location.pathname, '../JavascriptSrc/CoolyuYtPlayerEnhanced.js', '../JavascriptSrc/Offline.js', '../JavascriptSrc/sample.js', '../JavascriptSrc/gears_init.js', '../JavascriptSrc/jquery.js', '../JavascriptSrc/swfobject.js', '../player-viral.swf', '../preview.jpg', '../video.flv', '../youtube.png'];
var movieToCapture = ['../video.flv'];
var store;

function init(){
	hideYt();
    //check whether Google Gears installed.
    if (!window.google || !google.gears) {
        addStatus('Google Gears is not installed.', 'error');
		var s = $('#status').html();
		$('#status').html(s + "Please click <a href='http://gears.google.com'>here</a> to download.<br>"
		 + "(Google Gears is required to use Youtube Offline)");
        return;
    }
    var localServer = google.gears.factory.create('beta.localserver');
    /*
     var store = localServer.createManagedStore(STORE_NAME);
     store.manifestUrl = '../YtPlayer/manifest?version=3';
     store.checkForUpdate();
     */
    store = localServer.createStore(STORE_NAME);
    addStatus('Capturing...');
    //alert(movieToCapture[0]);
    //store.captureBlob(store.getAsBlob(movieToCapture[0]),movieToCapture[0],"application/x-shockwave-flash");
    store.capture(filesToCapture, captureCallback);
    
    //alert("cap");
    
}

function captureCallback(url, success, captureId){
    addStatus(url + ' captured ' + (success ? 'succeeded' : 'failed'));
	clearStatus();
    if (url == '../youtube.png') {
        setTimeout(function(){
            clearStatus();
            addStatus("Update Finished!!");
        }, 2000);
    }
}

function hideYt(){
    $("#displayPanel0").hide();
}

function showYt(){
    $("#displayPanel0").show();
}

function desktop2(){
    //alert("desktop");
    var desktop = google.gears.factory.create('beta.desktop');
    desktop.createShortcut('YoutubeOffline', 'http://coolyulab.coolyu.twbbs.org/', {
        '48x48': 'http://coolyulab.coolyu.twbbs.org/youtube.png'
    }, 'Youtube Offline made by Bob');
    //alert("desktop");
}

function download(n){
    clearStatus();
    $.ajax({
        type: "GET",
        url: "YtDownloader/",
        data: "videoId=" + getVideoIdByURL($("#url" + n).val()),
        //dataType: "json" ,
        success: function(data){
            //addStatus("success1 ");
            //var n = 1;
            store.capture("YtDownloader/?flv=" + getVideoIdByURL($("#url" + n).val()), filesToCaptureCallback);
            addStatus("capturing...");
			//addStatus("<img src='ajax-loader.gif'>");
			var s = $('#status').html();
			$('#status').html(s + "<img src='ajax-loader.gif'><br>");
            
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert("Fail to download flv file= =!!")
        }
    });
}

function filesToCaptureCallback(url, success, captureId){
	clearStatus();
    addStatus(url + ' captured ' + (success ? 'succeeded' : 'failed'));
    if (success == true) {
        //alert("success");
        var rename = url.substring(18, 30) + ".flv";
        store.rename(url, rename);
        setTimeout(function(){
            clearStatus();
            addStatus("You can now watch it on offline panel!!:)");
            hideYt();
            var rs = db.execute('select * from Test');
            var exist = false;
            while (rs.isValidRow()) {
                if (rs.field(0) == rename) {
                    exist = true;
                    break;
                }
                rs.next();
            }
            if (exist == false) {
                db.execute('insert into Test values (?)', [rename]);
            }
			updateList();
            initPlayer(rename, true);
            
        }, 1500);
        
    }
    
}
